package com.snake.excel2java.excel;

import com.snake.excel2java.excel.split.SplitConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

@Slf4j
public abstract class ExcelRowLoader implements IExcelRowLoader {

	// 加载Excel
	@Override
	public void load(XSSFRow row, int[] dataIndex) {
			
		doLoad(row, dataIndex);
		parse();
		
	}
	
	// 加载Excel
	protected void doLoad(XSSFRow row, int[] dataIndex) {
		
	}
	
	// 额外的解析, Ext文件中手动添加
	protected void parse() {
		
	}
	
	// 读取 boolean
	protected boolean readBool(XSSFRow row, int index) {	
		
		try {
			XSSFCell cell = row.getCell(index);

			if(null == cell)
				return false;
			
			switch (cell.getCellType()) {
			case BLANK:
				
				return false;
				
			case STRING: 
				
//				String value = cell.getStringCellValue();
//
//				if(null == value)
//					return false;
//
//				return Boolean.parseBoolean(value.trim());
				
			case NUMERIC: 
				
				int boolInt = readInt(row, index);
				
				return 0 != boolInt;
				
			default:
				break;
			}
			
			return cell.getBooleanCellValue();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("bool - read column {} error in row {}......", index, row.getRowNum());
		}
		
		return false;
	}
	
	// 读取 int
	protected int readInt(XSSFRow row, int index) {	
		
		try {
			XSSFCell cell = row.getCell(index);
			
			
			if(null == cell)
				return 0;
			
			switch (cell.getCellType()) {
			case BLANK:
				
				return 0;
				
			case STRING: 
				
				String value = cell.getStringCellValue();
				
				if(null == value)
					return 0;
				
				return Integer.parseInt(value.trim());
				
			default:
				break;
			}
			
			return Double.valueOf(cell.getNumericCellValue()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("int - read column {} error in row {}......", index, row.getRowNum());
		}
		
		return 0;
	}
	
	// 读取 float
	protected float readFloat(XSSFRow row, int index) {
		
		try {
			XSSFCell cell = row.getCell(index);
			
			if(null == cell)
				return 0f;
			
			switch (cell.getCellType()) {
			case BLANK:
				
				return 0f;
				
			case STRING: 
				
				String value = cell.getStringCellValue();
				
				if(null == value)
					return 0f;
				
				return Float.parseFloat(value.trim());

			default:
				break;
			}
			
			return Double.valueOf(cell.getNumericCellValue()).floatValue();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("float - read column {} error in row {}......", index, row.getRowNum());
		}
		
		return 0f;
		
	}
	
	// 读取 double
	protected double readDouble(XSSFRow row, int index) {
		
		try {
			XSSFCell cell = row.getCell(index);
			
			if(null == cell)
				return 0d;
			
			switch (cell.getCellType()) {
			case BLANK:
				
				return 0d;
				
			case STRING: 
				
				String value = cell.getStringCellValue();
				
				if(null == value)
					return 0d;
				
				return Double.parseDouble(value.trim());

			default:
				break;
			}
			
			return cell.getNumericCellValue();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("double - read column {} error in row {}......", index, row.getRowNum());
		}
		
		return 0d;
	}
	
	public static final String EMPTY = "";
	
	// 读取 String, 移除前后空格
	// 数组类型为String
	// 数组类型只包含一个，转换为String
	// 其他未识别的类型为String
	protected String readString(XSSFRow row, int index, StringType type) {
		
		try {
			XSSFCell cell = row.getCell(index);
			
			if(null == cell)
				return "";
			
			CellType cellType = cell.getCellType();
			
			switch (cellType) {
			case BLANK:
				
				return EMPTY;
				
			case BOOLEAN:
			
				return EMPTY + cell.getBooleanCellValue();
				
			case NUMERIC:
				
				switch (type) {
				case INT_ARRAY:
					
					return EMPTY + Double.valueOf(cell.getNumericCellValue()).intValue();
				case FLOA_TARRAY:
					
					return EMPTY + Double.valueOf(cell.getNumericCellValue()).floatValue();
				case DOUBLE_ARRAY:
					
					return EMPTY + cell.getNumericCellValue();

				default:
					return EMPTY + cell.getNumericCellValue();
						
				}
			

			default:
				break;
			}
			
			String value = cell.getStringCellValue();
			
			if(null == value)
				return "";
			
			return value.trim();
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("string - read column {} error in row {}......", index, row.getRowNum());
		}
		
		return EMPTY;
	}
	
	protected int[] split(String str) {
		
		if(str.isEmpty())
			return null;
		
		String[] temp = str.split(SplitConfig.COMMA);
		
		int[] result = new int[temp.length];
		int index = 0;
		
		while(index < temp.length)
			result[index] = Integer.parseInt(temp[index++]);
		
		return result;
	}
}
